package com.elective.courseselect.serviceUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elective.courseselect.entity.*;
import com.elective.courseselect.enums.CourseStatus;
import com.elective.courseselect.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author lee
 * @date 2021年09月19日 20:15
 */
@Component
public class CourseUtils {

    @Autowired
    UserService userService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseCharacterService courseCharacterService;

    @Autowired
    CharacterService characterService;

    @Autowired
    CommentService commentService;

    @Autowired
    CommentingService commentingService;

    @Autowired
    CourseQuestionnaireService courseQuestionnaireService;

    @Autowired
    QuestionnaireService questionnaireService;

    @Autowired
    StudentCourseService studentCourseService;

    public static CourseUtils courseUtils;

    @PostConstruct
    public void init(){
        courseUtils=this;   //必需
        courseUtils.userService=this.userService;
        courseUtils.courseService=this.courseService;
        courseUtils.courseCharacterService=this.courseCharacterService;
        courseUtils.commentService=this.commentService;
        courseUtils.commentingService=this.commentingService;
        courseUtils.courseQuestionnaireService=this.courseQuestionnaireService;
        courseUtils.questionnaireService=this.questionnaireService;
        courseUtils.studentCourseService=this.studentCourseService;
        courseUtils.courseUtils=this.courseUtils;
    }

    /**
     * 课程评论标签
     * @author Lee
     * @date 2021/9/19 20:34
     * @param courseEntity
     * @return com.elective.courseselect.entity.CourseEntity
     */
    public static CourseEntity getCourseLabels(CourseEntity courseEntity) {
        HashSet<String> tempLabels = new HashSet<>();
        Integer courseEntityId = courseEntity.getId();
        QueryWrapper<CourseCharacterEntity> courseCharacterEntityQueryWrapper = new QueryWrapper<>();
        courseCharacterEntityQueryWrapper.eq("course_id", courseEntityId);
        List<CourseCharacterEntity> courseCharacterEntityList = courseUtils.courseCharacterService.list(courseCharacterEntityQueryWrapper);
        if (null != courseCharacterEntityList) {
            for (CourseCharacterEntity courseCharacterEntity : courseCharacterEntityList) {
                Integer characterId = courseCharacterEntity.getCharacterId();
                CharacterEntity characterEntity = courseUtils.characterService.getById(characterId);
                tempLabels.add(characterEntity.getLabel());
            }
            courseEntity.setLabelNameList(tempLabels);
        }
        return courseEntity;
    }

    /**
     * 课程评论
     * @author Lee
     * @date 2021/9/19 20:34
     * @param courseEntity
     * @return com.elective.courseselect.entity.CourseEntity
     */
    public static CourseEntity getCourseComment(CourseEntity courseEntity) {
        HashSet<String> tempComments = new HashSet<>();
        Integer courseEntityId = courseEntity.getId();
        QueryWrapper<CommentingEntity> commentingEntityQueryWrapper = new QueryWrapper<>();
        commentingEntityQueryWrapper.eq("course_id", courseEntityId);
        //一门课会有多个学生评论
        List<CommentingEntity> list = courseUtils.commentingService.list(commentingEntityQueryWrapper);
        for (CommentingEntity commentingEntity : list) {
            Boolean abandon = UserUtils.isAbandon(commentingEntity.getStudentId());
            if (abandon) {
                continue;
            }
            if (null != commentingEntity.getCommentId()) {
                for (String commentId : commentingEntity.getCommentId().split(",")) {
                    if ("".equals(commentId) || StringUtils.isEmpty(commentId)) {
                        continue;
                    }else {
                        CommentEntity commentEntity = courseUtils.commentService.getById(commentId);
                        tempComments.add(commentEntity.getLabel());
                    }
                }
            }
        }
        courseEntity.setCommentList(tempComments);
        return courseEntity;
    }


    /**
     * 课程调查问卷
     * @author Lee
     * @date 2021/9/19 20:35
     * @param courseEntity
     * @return com.elective.courseselect.entity.CourseEntity
     */
    public static CourseEntity getCourseQuestionnaire(CourseEntity courseEntity) {
        ArrayList<QuestionnaireEntity> questionnaireEntities = new ArrayList<>();
        Integer courseEntityId = courseEntity.getId();
        QueryWrapper<CourseQuestionnaireEntity> courseQuestionnaireEntityQueryWrapper = new QueryWrapper<>();
        courseQuestionnaireEntityQueryWrapper.eq("course_id", courseEntityId);

        List<CourseQuestionnaireEntity> courseQuestionnaireEntities = courseUtils.courseQuestionnaireService.list(courseQuestionnaireEntityQueryWrapper);
        for (CourseQuestionnaireEntity courseQuestionnaireEntity : courseQuestionnaireEntities) {
            Integer questionnaireId = courseQuestionnaireEntity.getQuestionnaireId();
            QuestionnaireEntity questionnaireEntity = courseUtils.questionnaireService.getById(questionnaireId);
            questionnaireEntities.add(questionnaireEntity);
        }
        courseEntity.setQuestionnaireList(questionnaireEntities);
        return courseEntity;
    }


    /**
     * 得到课程余量
     * @author Lee
     * @date 2021/10/2 22:49
     * @param courseEntity
     * @return com.elective.courseselect.entity.CourseEntity
     */
    public static CourseEntity getCourseNumRemaining(CourseEntity courseEntity) {
        Integer choosePeronNum = 0;
        List<StudentCourseEntity> list = courseUtils.studentCourseService.list();
        for (StudentCourseEntity entity : list) {
            for (String result : entity.getCourseId().split(",")) {
                if (Integer.valueOf(result) == courseEntity.getId()) {
                    choosePeronNum++;
                }
            }
        }
        if (null != courseEntity.getAmount()) {
            courseEntity.setNumRemaining(courseEntity.getAmount() - choosePeronNum);
        }else{
            courseEntity.setNumRemaining(0);
        }
        return courseEntity;
    }

    /**
     * 判断课程是否被禁用
     * @author Lee
     * @date 2021/10/24 2:02
     * @param courseId
     * @return java.lang.Boolean
     */
    public static Boolean courseIsAbandon(Integer courseId) {
        CourseEntity courseEntity = courseUtils.courseService.getById(courseId);
        if (null == courseEntity || null == courseEntity.getStatus() || CourseStatus.NORMAL != courseEntity.getStatus()) {
            return true;
        }else{
            return false;
        }
    }


    /**
     * 老师能看的课程
     * @author Lee
     * @date 2021/10/24 2:02
     * @param courseId
     * @return java.lang.Boolean
     */
    public static Boolean courseLookByTea(Integer courseId) {
        CourseEntity courseEntity = courseUtils.courseService.getById(courseId);
        if (null == courseEntity || null == courseEntity.getStatus() || CourseStatus.COURSE_FORBID == courseEntity.getStatus()) {
            return true;
        }else{
            return false;
        }
    }



}
